package com.spark.lola.logistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.spark.lola.logistics.entity.ExpensesCategory;
import com.spark.lola.logistics.mapper.ExpensesCategoryMapper;
import com.spark.lola.logistics.service.ExpensesCategoryService;
import com.spark.lola.support.common.constant.CommonConstant;
import com.spark.lola.support.core.vo.SelectMap;
import com.spark.lola.support.mybatis.entity.PageParam;
import com.spark.lola.support.mybatis.entity.QueryParam;
import com.spark.lola.support.mybatis.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Zhi-peng wang
 * @date: 2020/05/06 13:02
 * 货运支出单分类业务实现
 **/
@Service
public class ExpensesCategoryServiceImpl extends BaseServiceImpl<ExpensesCategoryMapper, ExpensesCategory> implements ExpensesCategoryService {

    @Autowired
    private ExpensesCategoryMapper expensesCategoryMapper;

    @Override
    public IPage<ExpensesCategory> findPage(QueryParam<ExpensesCategory> queryParam) {
        PageParam<ExpensesCategory> pageParam = queryParam.toPageParam();
        return expensesCategoryMapper.findPage(pageParam.getPage(), pageParam.getEntityWrapper());
    }

    @Override
    public List<SelectMap> selectMap(String key) {
        QueryWrapper<ExpensesCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id,name");
        queryWrapper.eq("used", CommonConstant.Y);
        if (!StringUtils.isEmpty(key)) {
            queryWrapper.and(o -> o.like("no", key));
        }
        queryWrapper.orderByAsc("sort");
        List<ExpensesCategory> lst = baseMapper.selectList(queryWrapper);
        List<SelectMap> selectMaps = new ArrayList<>();
        if (!CollectionUtils.isEmpty(lst)) {
            lst.forEach(item -> {
                selectMaps.add(new SelectMap(item.getId(), item.getName()));
            });
        }
        return selectMaps;
    }

    @Override
    public List<SelectMap> findBi() {
        QueryWrapper<ExpensesCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id,name");
        queryWrapper.eq("used", CommonConstant.Y);
        queryWrapper.orderByAsc("sort");
        List<ExpensesCategory> lst = baseMapper.selectList(queryWrapper);
        List<SelectMap> selectMaps = new ArrayList<>();
        if (!CollectionUtils.isEmpty(lst)) {
            lst.forEach(item -> {
                selectMaps.add(new SelectMap(item.getId(), item.getName()));
            });
        }
        selectMaps.add(new SelectMap(110L, "合计"));
        return selectMaps;
    }
}
